<?php

require_once dirname(__FILE__).'/../lib/clubGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/clubGeneratorHelper.class.php';

/**
 * club actions.
 *
 * @package    cpfaizenay
 * @subpackage club
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class clubActions extends autoClubActions
{
    private $titleBC = 'Clubs';
    private $actionNewBC = 'Nouveau';
    private $actionEditBC = 'Modifier';
    private $url = 'club';

    public function executeIndex(sfWebRequest $request) {
        parent::executeIndex($request);
        $this->showBreadCrumb();
    }

    public function executeNew(sfWebRequest $request) {
        parent::executeNew($request);
        $this->showBreadCrumb($this->actionNewBC);
    }

    public function executeCreate(sfWebRequest $request) {
        parent::executeCreate($request);
        $this->showBreadCrumb($this->actionNewBC);
    }

    public function executeEdit(sfWebRequest $request) {
        parent::executeEdit($request);
        $this->showBreadCrumb($this->actionEditBC);
    }

    public function executeUpdate(sfWebRequest $request) {
        parent::executeUpdate($request);
        $this->showBreadCrumb($this->actionEditBC);
    }

    private function showBreadCrumb($action = NULL){
        if(!empty($action)){
            isicsBreadcrumbs::getInstance()->addItem($this->titleBC, $this->getController()->genUrl($this->url) );
            isicsBreadcrumbs::getInstance()->addItem($action, null);
        }else{
            isicsBreadcrumbs::getInstance()->addItem($this->titleBC, null);
        }
    }





    public function executeAutocompleteClub(sfWebRequest $request) {
      $this->forward404unless($request->isXmlHttpRequest());

      $this->getResponse()->setContentType ( 'application/json' );
      $results = Doctrine::getTable('Club')->retrieveClub($request->getParameter( 'q' ), $request->getParameter('limit'))->execute();

      $clubs = array();
      foreach ($results as $row) {
        $clubs[$row->getId()] = $row->getClubNumber() . " - " . $row->getName();
      }

      return $this->renderText (json_encode ($clubs));
   }

   public function executeAutocompleteCity(sfWebRequest $request) {
      $this->forward404unless($request->isXmlHttpRequest());

      $this->getResponse()->setContentType ( 'application/json' );
      $results = Doctrine::getTable('City')->retrieveCity($request->getParameter( 'q' ), $request->getParameter('limit'))->execute();

      $cities = array();
      foreach ($results as $row) {
        $cities[$row->getId()] = $row->getRawName();
      }

      return $this->renderText (json_encode ($cities));
   }

}
